package com.ruiyu.utils

import java.util.*
import java.util.regex.Pattern

//首字母转大写
fun String.toUpperCaseFirstOne(): String {
    return when {
        isEmpty() -> ""
        Character.isUpperCase(this[0]) -> this
        else -> StringBuilder().append(Character.toUpperCase(this[0])).append(this.substring(1)).toString()
    }
}

//首字母转小写
fun String.toLowerCaseFirstOne(): String {
    return if (Character.isLowerCase(this[0]))
        this
    else
        StringBuilder().append(Character.toLowerCase(this[0])).append(this.substring(1)).toString()
}

//大写字母转下划线和小写
fun String.upperCharToUnderLine(): String {
    val p = Pattern.compile("[A-Z]")
    if (this == "") {
        return ""
    }
    val builder = StringBuilder(this)
    val mc = p.matcher(this)
    var i = 0
    while (mc.find()) {
        builder.replace(mc.start() + i, mc.end() + i, "_" + mc.group().lowercase(Locale.getDefault()))
        i++
    }
    if ('_' == builder[0]) {
        builder.deleteCharAt(0)
    }
    return builder.toString()
}

//下划线和小写转大写字母
fun upperTable(str: String): String {
    // 字符串缓冲区
    val sbf = StringBuffer()
    // 如果字符串包含 下划线
    if (str.contains("_")) {
        // 按下划线来切割字符串为数组
        val split = str.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
        // 循环数组操作其中的字符串
        var i = 0
        val index = split.size
        while (i < index) {
            // 递归调用本方法
            val upperTable = upperTable(split[i])
            // 添加到字符串缓冲区
            sbf.append(upperTable)
            i++
        }
    } else {// 字符串不包含下划线
        // 转换成字符数组
        val ch = str.toCharArray()
        // 判断首字母是否是字母
        if (ch[0] in 'a'..'z') {
            // 利用ASCII码实现大写
            ch[0] = (ch[0].code - 32).toChar()
        }
        // 添加进字符串缓存区
        sbf.append(ch)
    }
    // 返回
    return sbf.toString()
}

// 创建jsonList的解析方法
fun createjsonListParseFunc(name:String):String {
    val sb = StringBuffer()
    sb.appendLine("\t/// 解析数组json\n" +
            "\t/// sub 解析data其子项\n" +
            "\t/// sub == null, data 必须是数组的json\n" +
            "\t/// sub != null, data 必须是map的json")
    sb.appendLine(
            "\tstatic List<$name> formList(dynamic data, [String? sub]) {\n" +
                    "\t\tvar ll = <$name>[];\n" +
                    "\n" +
                    "\t\ttry {\n" +
                    "\t\t\tvar jsonArray;\n" +
                    "\t\t\tif (data is String) {\n" +
                    "\t\t\t\tjsonArray = sub == null ? json.decode(data) : json.decode(data)[sub];\n" +
                    "\t\t\t} else {\n" +
                    "\t\t\t\tjsonArray = sub == null ? data : data[sub];\n" +
                    "\t\t\t}\n" +
                    "\n" +
                    "\t\t\tif (jsonArray != null) {\n" +
                    "\t\t\t\tfor (var json in jsonArray) {\n" +
                    "\t\t\t\t\tll.add($name.fromMap(json));\n" +
                    "\t\t\t\t}\n" +
                    "\t\t\t}\n" +
                    "\t\t} catch (e) {\n" +
                    "\t\t\tprint('json解析错误，错误类型：\${e.runtimeType}');\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\t\treturn ll;\n" +
                    "\t}"
    )
    return sb.toString()
}


// 创建json的工厂解析方法
fun createJsonFacParesFunc(name:String):String {
    val sb = StringBuffer()
    sb.append(
            "\t/// 解析对象json\n" +
                    "\t/// sub 解析data其子项\n" +
                    "\tstatic $name? parseJson(data, [String? sub]) {\n" +
                    "\t\tif (data == null) return null;\n" +
                    "\n" +
                    "\t\ttry {\n" +
                    "\t\t\tvar jsonObj;\n" +
                    "\t\t\tif (data is String) {\n" +
                    "\t\t\t\tjsonObj = sub == null ? json.decode(data) : json.decode(data)[sub];\n" +
                    "\t\t\t} else {\n" +
                    "\t\t\t\tjsonObj = sub == null ? data : data[sub];\n" +
                    "\t\t\t}\n" +
                    "\t\t\tif (jsonObj != null) return $name.fromMap(jsonObj);\n" +
                    "\t\t} catch (e) {\n" +
                    "\t\t\tprint('json解析错误，错误类型：\${e.runtimeType}');\n" +
                    "\t\t}\n" +
                    "\n" +
                    "\t\treturn null;\n" +
                    "\t}"
    )
    return sb.toString()
}